package com.gvfish.backingbean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.springframework.context.ApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import com.gvfish.core.Page;
import com.gvfish.entity.Article;
import com.gvfish.entity.Category;
import com.gvfish.service.ArticleManager;
import com.gvfish.service.CategoryManager;

@ManagedBean
@RequestScoped
public class CategoryShow {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Long categoryId;
	private Category category;

	private Page<Article> page = new Page<Article>(20);
	private Integer pageNo;

	public void setCategoryId(Long categoryId) {
		System.out.println("in set categoryId"+categoryId);
		this.categoryId = categoryId;
	}

	public Long getCategoryId() {
		return categoryId;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public Category getCategory() {
		if (category == null) {
			category = getCategoryManager().find(categoryId);
		}
		return category;
	}

	/**
	 * 获取下页数据
	 * 
	 * @param event
	 */
	public void getNextPage(ActionEvent event) {
		System.out.println("in getNextPage");
		page.setPageNo(page.getPageNo() + 1);
		getArticleManager().getByPage(categoryId, page);// 数据已经在page的result中
	}

	/**
	 * 获取上一页数据
	 * 
	 * @param event
	 */
	public void getPrePage(ActionEvent event) {
		System.out.println("in getPretPage");
		System.out.println(page);
		page.setPageNo(page.getPageNo() - 1);
		System.out.println(page);
		getArticleManager().getByPage(categoryId, page);

	}

	public void setPage(Page<Article> page) {
		this.page = page;
	}

	public Page<Article> getPage() {
		if (page == null) {
			System.out.println("create new Page.");
			page = new Page<Article>(20);
		}
		if (page.getResult().size() == 0) {
			page = getArticleManager().getByPage(categoryId, page);
		}
		return page;
	}

	public ArticleManager getArticleManager() {
		ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		ArticleManager articleManager = (ArticleManager) ctx.getBean("articleManager");
		return articleManager;
	}

	public CategoryManager getCategoryManager() {
		ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		CategoryManager categoryManager = (CategoryManager) ctx.getBean("categoryManager");
		return categoryManager;
	}

	public void setPageNo(Integer pageNo) {
		System.out.println("in setPageNo");
		page = new Page<Article>(20);
		page.pageNo(pageNo);
		this.pageNo = pageNo;
	}

	public Integer getPageNo() {
		return pageNo;
	}
	public void response(){
		System.out.println("in response");
		Category  c = getCategory();
		System.out.println(c);
		int count = c.getArtCount();
		page.setTotalCount(count);
	}
}